iT邦幫忙

2022 iThome 鐵人賽

DAY 23
0

You're a big model train enthusiast and have decided to share your passion with the world by starting a newsletter.

You'll start by sending the first issue of your newsletter to your friends and acquaintances that share your hobby. You have a text file with a list of their email addresses.

1. Read email addresses from a file

Implement the Newsletter.read_emails/1 function. It should take a file path. The file is a text file that contains email addresses separated by newlines. The function should return a list of the email addresses from the file.

2. Open a log file for writing

Sending an email is a task that might fail for many unpredictable reasons, like a typo in the email address or temporary network issues. To ensure that you can retry sending the emails to all your friends without sending duplicates, you need to log the email addresses that already received the email. For this, you'll need a log file.

Implement the Newsletter.open_log/1 function. It should take a file path, open the file for writing, and return the PID of the process that handles the file.

3. Log a sent email

Implement the Newsletter.log_sent_email/2 function. It should take a PID of the process that handles the file and a string with the email address. It should write the email address to the file, followed by a newline.

4. Close the log file

Implement the Newsletter.close_log/1 function. It should take a PID of the process that handles the file and close the file.

5. Send the newsletter

Now that you have all of the building blocks of the email sending procedure, you need to combine them together in a single function.

Implement the Newsletter.send_newsletter/3 function. It should take a path of the file with email addresses, a path of a log file, and an anonymous function that sends an email to a given email address.

It should read all the email addresses from the given file and attempt to send an email to every one of them. If the anonymous function that sends the email returns :ok, write the email address to the log file, followed by a new line. Make sure to do it as soon as the email is sent. Afterwards, close the log file.


https://exercism.org/tracks/elixir/exercises/newsletter

defmodule Newsletter do
  @moduledoc """
  practice file related
  """

  def read_emails(path), do: path |> File.read! |> String.split("\n", trim: true)

  def open_log(path), do: path |> File.open!([:write])

  def log_sent_email(pid, email), do: pid |> IO.puts(email)

  def close_log(pid), do: pid |> File.close

  def send_newsletter(emails_path, log_path, send_fun) do
    log_pid = log_path |> open_log

    emails_path
    |> read_emails
    |> Enum.each(fn email -> if (send_fun.(email) === :ok), do: log_sent_email(log_pid, email) end)

    log_pid |> close_log
  end
end

上一篇
二十二. Binaries
下一篇
二十四. Ranges
系列文
戀戀 Elixir30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言